package com.aaa.service.impl;

import com.aaa.dao.OperLogDao;
import com.aaa.dao.RegisteredItemDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.Notice;
import com.aaa.entity.OperLog;
import com.aaa.entity.RegisteredItem;
import com.aaa.entity.User;
import com.aaa.service.RegisteredItemService;
import com.aaa.vo.NoticeVo;
import com.aaa.vo.RegisteredVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class RegisteredItemServiceImpl extends ServiceImpl<RegisteredItemDao, RegisteredItem> implements RegisteredItemService {
    @Autowired
    private RegisteredItemDao registeredItemDao;
    @Override
    public Result<IPage<RegisteredItem>> findByConditions(Integer currentPage, Integer pageSize, RegisteredVo registeredVo) {
        IPage<RegisteredItem> page=new Page<>(currentPage,pageSize);
        QueryWrapper<RegisteredItem> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(registeredVo.getReg_item_name())){
            wrapper.like("reg_item_name",registeredVo.getReg_item_name());
        }
        if(Objects.nonNull(registeredVo.getReg_item_id())){
            wrapper.eq("reg_item_id",registeredVo.getReg_item_id());
        }
        if(Objects.nonNull(registeredVo.getReg_item_fee())){
            wrapper.eq("reg_item_fee",registeredVo.getReg_item_fee());
        }
        if(Objects.nonNull(registeredVo.getStatus())){
            wrapper.eq("status",registeredVo.getStatus());
        }
        if(Objects.nonNull(registeredVo.getDateRange())&&registeredVo.getDateRange().length==2){
            wrapper.between("create_time",registeredVo.getDateRange()[0],registeredVo.getDateRange()[1]);
        }

        IPage<RegisteredItem> page1 = registeredItemDao.selectPage(page, wrapper);
        return new Result<IPage<RegisteredItem>>(2000,"查询用户成功",page1);
    }

    //添加修改
    @Override
    public boolean saveAndUpdate(RegisteredItem RegisteredItem) {
        int i=-1;
        if (RegisteredItem.getCreateTime()==null){
            RegisteredItem.setCreateTime(new Date());
            RegisteredItem.setUpdateTime(new Date());
        }else {
            RegisteredItem.setUpdateTime(new Date());
        }
        if(RegisteredItem.getRegItemId()==null){
            i=registeredItemDao.insert(RegisteredItem);
            System.out.println("====="+RegisteredItem.getStatus());
        }else {
            i=registeredItemDao.updateById(RegisteredItem);
        }
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delById(Long id) {
        int i = registeredItemDao.deleteById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }

    //模块接口
    @Override
    public List<RegisteredItem> getAllRegisteredItems() {
        QueryWrapper<RegisteredItem> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        return registeredItemDao.selectList(wrapper);
    }
}
